/**
 * Created with Vim7.3 Mac
 * @fileOverview : 
 * @author : Lyle lylechen2014@gmail.com
 * @since : 2016-09-06 17:40:12
 * @filename : src/base/components/head.js
 * @version : 
 * @description : 
 */
define(function(require, exports, module) {

    var tpl = require('{base}/tpls/head.tpl');

    var head = {

        template: _.template(tpl),

        render: function(page, obj) {
            this.page = page;
            this.getAjaxStatus();
            return this.template(this.getTplData());
        },

        componentMount: function() {
            this.bindEvt();
        },

        componentUpdate: function() {
            $('#' + this.page.id).find('.head').replaceWith(this.template(this.getTplData()));
            this.bindEvt();
        },

        resetConfig: function() {
            var userStatus = JSON.parse(Util.getSessionStorage('userStatus'));
            if (userStatus && (userStatus.winked_count.new_count > 0 || userStatus.viewed_count.new_count > 0)) {
                if (Config.NavigationMenu.MyLists) {
                    Config.NavigationMenu.MyLists.type = 1;
                    Config.NavigationMenu.MyLists.list.WinkedAtMe.num = userStatus.winked_count.new_count;
                    Config.NavigationMenu.MyLists.list.EmailedMe.num = userStatus.emailed_count.new_count;
                    Config.NavigationMenu.MyLists.list.ViewedMe.num = userStatus.viewed_count.new_count;
                    Config.NavigationMenu.MyLists.list.RequestedMyPhoto.num = userStatus.requested_photo_count.new_count;
                    Config.NavigationMenu.MyLists.list.EmailedMe.num = userStatus.emailed_count.new_count;
                    Config.NavigationMenu.MyLists.list.InterestedInMe.num = userStatus.interested_count.new_count;
                    //todo
                };
            }
        },

        getTplData: function() {
            var obj = {
                menu: Config.NavigationMenu,
                isGuest: 1
            };
            this.resetConfig();
            var userInfo = Share.getUserInfo();
            if (userInfo && userInfo.status.isGuest == 0) {
                obj.menu = JSON.parse(JSON.stringify(obj.menu).replace(/\"locked\"\:true/ig, '"locked": false'));
                obj.isGuest = 1;
            }
            return obj;
        },

        getAjaxStatus: function() {
            var tthis = this,
                callback = function() {
                    tthis.componentUpdate();
                }
            Share.getUserStatus(callback);
        },

        bindEvt: function() {
            var tthis = this,
                Dom = $('#' + this.page.id).find('.head');

            Dom.find(".menuName").on("mouseover", function() {
                $(this).siblings().find('.menuList').hide();
                $(this).find('.menuList').show();
            });

            Dom.find(".menuName").on("mouseleave", function() {

                $(this).find('.menuList').hide();
            });

            Dom.find(".menuListItem").on("mouseover", function() {
                var twoMenu = $(this).find('dl');
                if (twoMenu.length > 0) {
                    twoMenu.show()
                }
            });

            Dom.find(".menuName").on("mouseleave", function() {
                var twoMenu = $(this).find('dl');
                if (twoMenu.length > 0) {
                    twoMenu.hide()
                }
            });

            Dom.find(".menuName").on("click", function() {
                var url = $(this).attr('url');
                if (url) {
                    //window.open(url);
                    alert(url);
                }
            });

            Dom.find(".menuListItem").on("click", function() {
                var url = $(this).attr('url');
                if (url) {
                    //window.open(url);
                    alert(url);
                }
            });

        },
    }

    module.exports = head;
});
